package com.mfnote.view.paint;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.view.ContextThemeWrapper;
import android.view.Display;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import com.mfnote.db.dao.HandDao;
import com.mfnote.db.dao.PaintDao;
import com.mfnote.db.dao.WriteDao;
import com.mfnote.util.FinalData;
import com.mfnote.util.ShareUtil;
import com.mfnote.util.image.ImageManager;
import com.mfnote.view.R;
import com.mfnote.view.R.anim;
import com.mfnote.view.R.id;
import com.mfnote.view.R.layout;
import com.mfnote.view.main.MainActivity;
import com.mfnote.view.other.MyApplication;

public class PaintActivity extends Activity implements OnClickListener {

	Button btnAdd=null; //顶部添加按钮
	ListView lv_paint=null; //涂鸦记录列表
	ArrayList<HashMap<String, Object>> data = null; //列表数据
	SimpleAdapter adapter = null; //涂鸦记录适配
	public static PaintActivity instance=null; //创建实例
	private AlertDialog alertDlgIsDel; //长按记录弹出删除提示框
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.paint);
		MyApplication.getInstance().addActivity(this); //加入Activity集合
		instance=this;
		
		btnAdd=(Button) findViewById(R.id.paint_titlebar_btn_add);
		btnAdd.setOnClickListener(this); //添加按钮监听
		
		loadPaintList();
		
	}
	
	/** 点击添加按钮跳转到涂鸦界面 */
	@Override
	public void onClick(View v) {
		Intent intent=new Intent(this,NewPaintActivity.class);
		startActivity(intent);
		finish();
		overridePendingTransition(R.anim.push_right_in,
				R.anim.push_left_out);
	}
	
	/** 加载涂鸦记录 */
	private void loadPaintList() {

		lv_paint=(ListView) findViewById(R.id.paint_lv);
		
		data=new PaintDao(this).getPaintAllCount();
		adapter=new SimpleAdapter(this,data,R.layout.paint_listview_item,new String[]{"title","time"},new int[]{R.id.paint_lv_title,R.id.paint_lv_time});
		lv_paint.setAdapter(adapter);
		
		lv_paint.setOnItemClickListener(new OnItemClickListener() {

			@SuppressWarnings("unchecked")
			@Override
			public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
				HashMap<String,Object> map=(HashMap<String, Object>) parent.getItemAtPosition(position);
				File file=new File((String) map.get("imagePath"));
				if(map.get("title").equals("点我开始涂鸦")||file.exists()){
					Intent intent=new Intent(PaintActivity.this,NewPaintActivity.class);
					intent.putExtra("paintmap", map);
					//requestCode为1---代表请求跳转到NewPaintActivity界面
					startActivityForResult(intent,1);
					finish();
					overridePendingTransition(R.anim.push_right_in,
							R.anim.push_left_out);
				}else{
					Toast.makeText(PaintActivity.this, "文件不存在", Toast.LENGTH_SHORT).show();
					new PaintDao(PaintActivity.this).deletePaint(map);
					loadPaintList();//更新一下列表
				}
			}
		});
		lv_paint.setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> parent, View view,
					int position, long id) {
				//此处弹出对话框提示是否删除此记录
				@SuppressWarnings("unchecked")
				HashMap<String, Object> map = (HashMap<String, Object>) parent.getItemAtPosition(position);
				
				if(!map.get("title").equals("点我开始涂鸦")) {  //第一条记录不能被删掉哦，谨记
					//打开删除对话框
					openDialog(map);
				}
				return false;
			}
		});
		
	}
	
	/**长按弹出操作对话框**/
	private void openDialog(final HashMap<String, Object> map) {
		
		//自定义Context,添加主题
		Context dialogContext = new ContextThemeWrapper(PaintActivity.this,android.R.style.Theme_DeviceDefault_Light_DarkActionBar);
		String[] choiceItems= new String[3];
		choiceItems[0] = "编辑";  
		choiceItems[1] = "删除";
		choiceItems[2] = "分享";  
		ListAdapter adapter = new ArrayAdapter<String>(dialogContext, android.R.layout.simple_list_item_1,choiceItems);
		//对话框建立在刚才定义好的上下文上
		AlertDialog.Builder builder = new AlertDialog.Builder(dialogContext);
		builder.setTitle("您要进行的操作");
		builder.setSingleChoiceItems(adapter, -1, new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				switch (which) {
				case 0:  //编辑
					Intent intent = new Intent(PaintActivity.this,NewPaintActivity.class);
					intent.putExtra("paintmap",map);
					//requestCode为1---》代表请求跳转到NewWriteActivity界面
					startActivityForResult(intent,1);
					finish();
					overridePendingTransition(R.anim.push_right_in,
							R.anim.push_left_out);
					break;
				case 1:  //删除
					loadDialog(map);
					break;
				case 2:  // 分享
					
					String saveimgPath = (String) map.get("imagePath");
					ShareUtil.share(saveimgPath, "快来观摩大神的杰作！", PaintActivity.this);
					break;
				}
				dialog.dismiss();
			}
		});
		builder.create().show();
		
	}
	
	/**弹出删除提示对话框**/
	private void loadDialog(final HashMap<String, Object> map) {
		//对传入的item的map进行剖析处理
		LayoutInflater inflater=LayoutInflater.from(this);
		View viewDialog=inflater.inflate(R.layout.dialog_isdelete, null);
		alertDlgIsDel= new AlertDialog.Builder(this).create();
		alertDlgIsDel.show();
		Display display=this.getWindowManager().getDefaultDisplay();
		int width=display.getWidth();
		int height=display.getHeight();
		LayoutParams layoutParams=new LayoutParams(width*90/100,LayoutParams.WRAP_CONTENT);	
		alertDlgIsDel.setContentView(viewDialog, layoutParams);
		Button btn_delete_sure=(Button) viewDialog.findViewById(R.id.btn_delete_sure);
		Button btn_delete_cancel=(Button) viewDialog.findViewById(R.id.btn_delete_cancel);
		btn_delete_sure.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				//调用数据库删除操作
//			long state = DBHelper.getInstance(WriteActivity.this).deleteWrite(map);
			long state = new PaintDao(PaintActivity.this).deletePaint(map);
			 //添加成功
			 if(state != -1) {
				 Toast.makeText(PaintActivity.this, "删除成功", Toast.LENGTH_LONG).show();
				 loadPaintList();//更新一下列表
			 } else {
				 Toast.makeText(PaintActivity.this, "删除失败", Toast.LENGTH_LONG).show();
			 }
				alertDlgIsDel.dismiss();
			}
		});
		btn_delete_cancel.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				alertDlgIsDel.dismiss();
			}
		});
	}

	/**按键处理**/
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if(keyCode == KeyEvent.KEYCODE_BACK) { //捕获Back按键
			
			Intent intent = new Intent(PaintActivity.this,MainActivity.class);
			startActivity(intent);
			this.finish();
			overridePendingTransition(R.anim.alpha_in,R.anim.scale_suoxiao_out
					);
		}
		return false;
	}

}
